var dataGridURL;
var addFormId;
var editFormId;
var moduleDataGridURL = "";
function newRegister() {
    dataGridURL = 'add?ajax=true';
    $('#dlg').dialog('open');
    $('div#divLoader').window('open');
    $.ajax({
        type: "GET",
        url: moduleDataGridURL+dataGridURL,
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        dataType: "html",
        success: function(result) {
            if (result === 'success') {
                $('#dlg').dialog('close');
                $('#datagrid').datagrid('reload');
            } else {
                $('#dlg .panel-body').html(result);		// close the dialog
                // disable the enter key
                setTimeout(function() {
                    $('#'+addFormId).submit(function() {
                        saveRegister();
                        return false;
                    });
                }, 1000);
            }
            $('div#divLoader').window('close');
        }
    });
}

function editRegister() {
    var row = $('#datagrid').datagrid('getSelected');
    if (row) {
        dataGridURL = 'edit/' + row.id + '?ajax=true';

        $('#dlg').dialog('open');
        $('div#divLoader').window('open');
        $.ajax({
            type: "GET",
            url: moduleDataGridURL+dataGridURL,
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            dataType: "html",
            success: function(result) {
                if (result === 'success') {
                    $('#dlg').dialog('close');
                    $('#datagrid').datagrid('reload');
                } else {
                    $('#dlg .panel-body').html(result);		// close the dialog
                    // disable the enter key
                    setTimeout(function() {
                        $('#'+editFormId).submit(function() {
                            saveRegister();
                            return false;
                        });
                    }, 1000);
                }
                $('div#divLoader').window('close');
            }
        });
    }
}
function saveRegister() {
    if ($("#fm").form('validate')) {
        $('div#divLoader').window('open');
        formId = "#" + (dataGridURL.substr(0, 1) == 'a' ? addFormId : editFormId);
        console.log(formId);
        try{// if is using CKEDITOR
            for (instance in CKEDITOR.instances)
                CKEDITOR.instances[instance].updateElement();
        }catch(e){
            
        }
        var formData = $(formId).serialize();
        $.ajax({
            type: "POST",
            url: moduleDataGridURL+dataGridURL,
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            data: formData,
            success: function(result) {
                if (result === 'success') {
                    $('#dlg').dialog('close');
                    $('#datagrid').datagrid('reload');
                } else {
                    $('#dlg .panel-body').html(result);		// close the dialog
                }
                $('div#divLoader').window('close');
            }
        });
    }

}

function removeRegister() {
    var row = $('#datagrid').datagrid('getSelected');
    if (row) {
        $('div#divLoader').window('open');
        $.messager.confirm('Delete', 'Are you sure?', function(r) {
            if (r) {
                eval('var p =  {"id":' + row.id + '}');
                $.ajax({
                    type: "POST",
                    url: moduleDataGridURL+'delete/' + row.id + '?ajax=true',
                    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                    dataType: "html",
                    data: $("#CoreBlockAddForm").serialize(),
                    success: function(result) {
                        $('#datagrid').datagrid('reload');
                    }
                });
            }
        });
        $('div#divLoader').window('close');
    }
}

function qq(value, name) {
    $('#datagrid').datagrid('load', {
        searchBoxValue: value,
        searchBoxName: name
    });
    //alert(value+":"+name)
}

$(function() {
    $('#datagrid').datagrid({
        fitColumns: true,
        autoRowHeight: false,
        url: moduleDataGridURL+'datagrid_data',
        toolbar: "#toolbar",
        pagination: true,
        rownumbers: true,
        striped: true,
        singleSelect: true,
        nowrap: false,
        pageSize: 20,
        pageList: [10,20,30,40,50,100,200],
        onLoadSuccess: function(data) {
            $('.easyui-linkbutton').linkbutton();
        },
        onDblClickRow: function(index, data) {
            $('#dg').datagrid('selectRow', index);
            editRegister();

            //editarPessoa(dados_form);
            //$('#result').load(result, '../Geral/CarregaCadastro/'+data['geralPessoa.gepeId']);

        }

    });
    dataGridResize('#datagrid', 500);


});